package com.achen.service.impl;

import com.achen.dao.MemberDao;
import com.achen.pojo.Member;
import com.achen.service.MemberService;
import com.achen.utils.MD5Utils;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author achen
 * @create 2020-04-21 22:41
 */
@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;

    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    @Override
    public void addMember(Member member) {
        //判断用户是自动注册还是通过其他方式注册,如果不是自动注册,会输入密码,这时候需要将密码加密
        String password = member.getPassword();
        if(password != null){
            //将明文密码进行加密
            password = MD5Utils.md5(password);
            member.setPassword(password);
        }
        memberDao.add(member);
    }

    @Override
    public List<Integer> findMemberCountByDate(List<String> list) {
        List<Integer> countList = new ArrayList<>();
        for(String month : list){
            month = month + ".31"; //每个月都默认小于31号
            Integer count = memberDao.findMemberCountBeforeDate(month); //每个月会员的数量
            System.out.println(count);
            //将所有月份的会员数量添加到集合中
            countList.add(count);
        }
        return countList;
    }
}
